package dk.nordahl.weatherstation;

import java.util.Timer;
import java.util.TimerTask;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Build;
import android.os.Bundle;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.widget.TextView;

@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public class MainActivity extends Activity {
	private SensorManager SM;
	private TextView temTV;
	private TextView presTV;
	private TextView lightTV;
	
	private float tempNow = Float.NaN;
	private float pressNow = Float.NaN;
	private float lightNow = Float.NaN;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		
		temTV = (TextView)findViewById(R.id.temperature);
		presTV = (TextView)findViewById(R.id.pressure);
		lightTV = (TextView)findViewById(R.id.light);
		SM = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
		Timer updateTimer = new Timer("weatherUpdate");
		updateTimer.scheduleAtFixedRate(new TimerTask() {
		public void run() {
		updateGUI();
		}
		}, 0, 1000);
	}
	private final SensorEventListener tempSensorEventListener = new SensorEventListener() {
		public void onAccuracyChanged(Sensor sensor, int accuracy) { }
		public void onSensorChanged(SensorEvent event) {
			tempNow = event.values[0];
		}
	};
	private final SensorEventListener pressureSensorEventListener = new SensorEventListener() {
		public void onAccuracyChanged(Sensor sensor, int accuracy) { }
		public void onSensorChanged(SensorEvent event) {
			pressNow = event.values[0];
		}
	};
	private final SensorEventListener lightSensorEventListener = new SensorEventListener() {
		public void onAccuracyChanged(Sensor sensor, int accuracy) { }
		public void onSensorChanged(SensorEvent event) {
			lightNow = event.values[0];
		}
	};
	@Override
	protected void onResume() {
	super.onResume();
	Sensor lightSensor = SM.getDefaultSensor(Sensor.TYPE_LIGHT);
	if (lightSensor != null)
		SM.registerListener(lightSensorEventListener,lightSensor,SensorManager.SENSOR_DELAY_NORMAL);
	else
		
	lightTV.setText("Light Sensor Unavailable");
	Sensor pressureSensor = SM.getDefaultSensor(Sensor.TYPE_PRESSURE);
	if (pressureSensor != null)
	SM.registerListener(pressureSensorEventListener, pressureSensor, SensorManager.SENSOR_DELAY_NORMAL);
	else
	presTV.setText("Barometer Unavailable");
	Sensor temperatureSensor = SM.getDefaultSensor(Sensor.TYPE_AMBIENT_TEMPERATURE);
	if (temperatureSensor != null)
	SM.registerListener(tempSensorEventListener,	temperatureSensor, SensorManager.SENSOR_DELAY_NORMAL);
	else
	temTV.setText("Thermometer Unavailable");
	}
	private void updateGUI() {
		runOnUiThread(new Runnable() {
		public void run() {
		if (!Float.isNaN(pressNow)) 
		{
			presTV.setText(pressNow + "hPa");
			presTV.invalidate();
		}
		if (!Float.isNaN(lightNow))
		{
			String lightStr = "Sunny";
		if (lightNow <= SensorManager.LIGHT_CLOUDY)
			lightStr = "Night";
		else if (lightNow <= SensorManager.LIGHT_OVERCAST)
			lightStr = "Cloudy";
		else if (lightNow <= SensorManager.LIGHT_SUNLIGHT)
			lightStr = "Overcast";
			lightTV.setText(lightStr);
			lightTV.invalidate();
		}
		if (!Float.isNaN(tempNow))
		{
			temTV.setText(tempNow + "C");
			temTV.invalidate();
		}
	}
	});
	};
	@Override
	protected void onPause() {
		SM.unregisterListener(pressureSensorEventListener);
		SM.unregisterListener(tempSensorEventListener);
		SM.unregisterListener(lightSensorEventListener);
		super.onPause();
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
